/**
 * @program: LeetCode
 * @description: LeetCode : 剑指 Offer II 001. 整数除法
 * @author: WXY
 * @create: 2022-12-18 20:58
 * @Version 1.0
 **/

public class offer_one1_divide {
    public static int divide(int a, int b) {
        int MIN = Integer.MIN_VALUE;
        int MAX = Integer.MAX_VALUE;
        if (a == MIN && b == -1) return MAX;
        boolean isPos = (a < 0 && b > 0) || (a > 0 && b < 0) ? true : false;
        if (a > 0) a = -a;
        if (b > 0) b = -b;
        int ans = 0;
        while (a <= b) {
            int d = b,c = 1;
            while (d >= MIN >> 1 && d + d >= a) {
                d += d;
                c += c;
            }
            a -= d;
            ans += c;
        }
        return isPos == true ? -ans : ans;
    }

    public static void main(String[] args) {
        int divide = divide(100, 7);
        System.out.println(divide);
    }
}
